<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">

    <title>传说中的上传功能</title>
    <link rel="icon" type="image/x-icon" href="/static/favicon.ico">
    <link rel="stylesheet" href="/static/css/global.css">

    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap-theme.min.css" rel="stylesheet">
    <link href="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet">

    <script src="https://cdn.bootcss.com/jquery/3.4.1/core.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.js"></script>

    <script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <script src="https://cdn.bootcss.com/twitter-bootstrap/3.4.1/js/npm.js"></script>

    <script src="https://cdn.bootcss.com/socket.io/2.3.0/socket.io.js"></script>
</head>

<body>
    <table class="table table-striped table-hover table-bordered">
        <thead>
            <tr>
                <th>设备名称</th>
                <th>设备ID</th>
                <th>操作</th>
            </tr>
        </thead>

        <tbody>
            {% for id in online_device %}
            {% if online_device[id] != 'upload page' and online_device[id] %}
            <tr>
                <td>{{ online_device[id] }}</td>
                <td>{{ id }}</td>
                <td><button class="btn btn-primary" onclick="upload('{{online_device[id]}}')">上传</button></td>
            </tr>
            {% endif %}
            {% endfor %}
        </tbody>
    </table>

    <a href="/home" class="btn btn-success">返回主页</a>

    <script>

        const socket = io();

        socket.on('connect', function () {
            socket.emit('first_connection', {'device name': 'upload page',});
        });

        socket.on('device change', function (msg) {
            console.log(msg);
            window.location.reload();
        });

        window.onbeforeunload = function () {
            socket.disconnect();
        };

        function upload(device_name) {
            var formdata = new FormData();
            formdata.append('device name', device_name);
            console.log(location.pathname);

            $.ajax({
                type: 'post',
                url: location.pathname + location.search,
                data: formdata,
                processData: false,
                contentType: false,
                success: function (msg) {
                    if (msg === 'success')
                    {
                        window.location.replace('/home');
                    }
                    else 
                    {
                        location.reload();
                    }
                },
                error: function () {
                    console.log('fuck!!!!!!!!!!');
                },
            });
        }
    </script>
</body>

</html>